package com.shijie.service.impl;

import com.shijie.constant.IdKeyPrefix;
import com.shijie.constant.PathConstant;
import com.shijie.dto.PoolAddDTO;
import com.shijie.dto.PoolUpdateDTO;
import com.shijie.entity.Pool;
import com.shijie.mapper.PoolMapper;
import com.shijie.service.PoolService;
import com.shijie.utlis.JsonUtils;
import com.shijie.utlis.RedisIdWorker;
import com.shijie.vo.JudgePoolVO;
import com.shijie.vo.PoolVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class PoolServiceImpl implements PoolService {

    private final PoolMapper poolMapper;

    private final RedisIdWorker redisIdWorker;

    @Override
    public List<PoolVO> list() {
        List<PoolVO> poolVOs = poolMapper.list();

        return poolVOs;
    }

    @Override
    public boolean save(PoolAddDTO poolAddDTO) {
        long poolId = redisIdWorker.nextId(IdKeyPrefix.POOL_KEY);
        log.info("--poolAddDTO save: {}", poolAddDTO);

        // 对输入/输出参数进行处理
        Path casepath = JsonUtils.writeJson2FilePath(PathConstant.CASE_DIRS, String.valueOf(poolId), poolAddDTO.getCases());
        Path resultpath = JsonUtils.writeJson2FilePath(PathConstant.RESULT_DIRS, String.valueOf(poolId), poolAddDTO.getResults());
        Pool pool = Pool.builder()
                .poolId(poolId)
                .casesPath(casepath.toString())
                .resultsPath(resultpath.toString())
                .build();
        BeanUtils.copyProperties(poolAddDTO, pool);

        String allType = JsonUtils.toJson(poolAddDTO.getAllType());
        pool.setAllType(allType);


        boolean ok = poolMapper.save(pool);

        return ok;
    }

    @Override
    public PoolVO getByPoolId(Long poolId) {
        return poolMapper.getByPoolId(poolId);
    }

    @Override
    public boolean update(PoolUpdateDTO poolUpdateDTO) {
        // 更新的输入示例和输出示例
        if (poolUpdateDTO.getCases() != null && poolUpdateDTO.getCases().isEmpty()
                && poolUpdateDTO.getResults() != null && poolUpdateDTO.getResults().isEmpty()) {
            Long poolId = poolUpdateDTO.getPoolId();

            JsonUtils.writeJson2FilePath(PathConstant.CASE_DIRS, String.valueOf(poolId), poolUpdateDTO.getCases());
            JsonUtils.writeJson2FilePath(PathConstant.RESULT_DIRS, String.valueOf(poolId), poolUpdateDTO.getResults());
        }

        // 更新其它
        if (!StringUtils.hasText(poolUpdateDTO.getTitle())
                && poolUpdateDTO.getDifficulty() == null
                && !StringUtils.hasText(poolUpdateDTO.getDescription())) return true;
        Pool pool = Pool.builder().build();
        BeanUtils.copyProperties(poolUpdateDTO, pool);

        return poolMapper.update(pool);
    }

    @Override
    public boolean delete(Long poolId) {
        boolean ok = poolMapper.delete(poolId);

        return ok;
    }

    @Override
    public JudgePoolVO getJudgeRequire(Long poolId) {
        Pool pool = poolMapper.getJudgeRequire(poolId);

        String[] allType = JsonUtils.fromJson(pool.getAllType(), String[].class);

        JudgePoolVO judgePoolVO = JudgePoolVO.builder()
                .allType(Pool.AllType.of(allType).getClazzs())
                .casePath(Paths.get(pool.getCasesPath()))
                .resultPath(Paths.get(pool.getResultsPath()))
                .build();

        return judgePoolVO;
    }


}
